package com.zzvcom.service.admin;

import java.io.File;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zzvcom.persist.model.admin.Image;
import com.zzvcom.template.hibernate.DefaultManager;

@Service
@Transactional
public class ImageManager extends DefaultManager<Image, String>
{
	public void deleteImageById(String id)
	{
		Image image = this.get(id);
		if(image==null)return ;
		String relative = image.getPath()
				.replaceAll("#", "\\" + File.separator);
		String path = ServletActionContext.getServletContext().getRealPath(
				relative);
		File file = new File(path);
		if (file.isFile())
		{
			file.delete();
		}
		String ext = path.substring(path.lastIndexOf("."));// 扩展名
		String pre = path.substring(0, path.lastIndexOf("."));// 扩展名以前
		File middle = new File(pre + "@middle" + ext);
		File small = new File(pre + "@small" + ext);
		if (middle.isFile())
		{
			middle.delete();
		}
		if (small.isFile())
		{
			small.delete();
		}
		this.delete(image);

	}

	public static void main(String agrs[])
	{
		String path = "ccc";
		String paths[] = path.split("@");
		System.out.println(paths.length);
		System.out.println(paths[0]);

	}

}
